﻿Option Explicit On
Option Strict On

Public NotInheritable Class ExceptionManager

#Region " Private Members "

    Private Shared s_ReportingEmailAddress As String = "James@m0yom.co.uk"
    Private Shared s_SystemName As String = "ACOM Director"
    Private Shared s_SystemVersion As String = "Unknown Version"

#End Region

#Region " Constructor "

    ''' <summary>
    ''' Private Constructor
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub New()
    End Sub

#End Region

#Region " Friend Properties "

    ''' <summary>
    ''' The Email address to send the error reports to
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Friend Shared Property ReportingEmailAddress() As String
        Get
            Return s_ReportingEmailAddress
        End Get
        Set(ByVal value As String)
            s_ReportingEmailAddress = value
        End Set
    End Property

    ''' <summary>
    ''' The Name of the System Using the Error Management
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Friend Shared Property SystemName() As String
        Get
            Return s_SystemName
        End Get
        Set(ByVal value As String)
            s_SystemName = value
        End Set
    End Property

    ''' <summary>
    ''' The Version of the Application Using the Error Management
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Friend Shared Property SystemVersion() As String
        Get
            Return s_SystemVersion
        End Get
        Set(ByVal value As String)
            s_SystemVersion = value
        End Set
    End Property

#End Region

#Region " Shared Methods "

    ''' <summary>
    ''' Publishes an exception
    ''' </summary>
    ''' <param name="ex"></param>
    ''' <param name="showUI"></param>
    ''' <remarks></remarks>
    Public Shared Sub PublishException(ByVal ex As Exception, ByVal showUI As Boolean)

        'Do not Process the Exception if we are running as a windows service
        If Not Environment.UserInteractive Then
            Throw ex
            Exit Sub
        End If

        If showUI Then

            'Display the Error Form to the user
            Using ErrorForm As New ExceptionReportForm
                Dim ExFormatter As New ExceptionFormatter

                ErrorForm.WebBrowser1.DocumentText = ExFormatter.GetHTMLError(ex, s_SystemName, s_SystemVersion)
                ErrorForm.Exception = ex
                ErrorForm.ShowDialog()
            End Using

        End If
    End Sub

#End Region

End Class
